define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'user/user/index',
                    add_url: 'user/user/add',
                    edit_url: 'user/user/edit',
                    del_url: 'user/user/del',
                    multi_url: 'user/user/multi',
                    table: 'user',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'user.id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), sortable: true},
                        {field: 'group.name', title: __('Group')},
                        {field: 'username', title: __('Username'), operate: 'LIKE'},
                        {field: 'nickname', title: __('Nickname'), operate: 'LIKE'},
                        {field: 'email', title: __('Email'), operate: 'LIKE'},
                        {field: 'mobile', title: __('Mobile'), operate: 'LIKE'},
                        {field: 'avatar', title: __('Avatar'), events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false},
                        {field: 'level', title: __('Level'), operate: 'BETWEEN', sortable: true},
                        {field: 'gender', title: __('Gender'), visible: false, searchList: {1: __('Male'), 0: __('Female')}},
                        {field: 'score', title: __('Score'), operate: 'BETWEEN', sortable: true},
                        {field: 'successions', title: __('Successions'), visible: false, operate: 'BETWEEN', sortable: true},
                        {field: 'maxsuccessions', title: __('Maxsuccessions'), visible: false, operate: 'BETWEEN', sortable: true},
                        {field: 'logintime', title: __('Logintime'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {field: 'loginip', title: __('Loginip'), formatter: Table.api.formatter.search},
                        {field: 'jointime', title: __('Jointime'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {field: 'joinip', title: __('Joinip'), formatter: Table.api.formatter.search},
                        {field: 'status', title: __('Status'), formatter: Table.api.formatter.status, searchList: {normal: __('Normal'), hidden: __('Hidden')}},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        select: function () {
            let vue = new Vue({
                el: '#vue',
                data: {
                    submitLoading: false,
                    levelList: [],
                    loadingText: '拼命加载中...',
                    // 添加编辑
                    title: '',
                    addDialog: false,
                    // 表格当前页数据
                    tableData: [],
                    // 多选数组
                    multipleSelection: [],
                    // 搜索条件
                    key: '',
                    // 默认每页数据量
                    pageSize: 20,
                    // 当前页码
                    currentPage: 1,
                    // 默认数据总数
                    totalCount: 0,
                    // 列表加载遮罩
                    listLoading: false,
                    // 搜索字段
                    search: {
                        startTime: '',
                        endTime: '',
                        keyword: '',
                        type: 1,
                        sort: 'id',
                    },
                    // 字段信息
                    formData: {},
                    searchOpt: [{
                        id: 1,
                        value: '用户名'
                    }, {
                        id: 2,
                        value: '用户昵称'
                    }, {
                        id: 3,
                        value: '手机号码'
                    }],
                    sendStartTime: '',
                    sendEndTime: '',
                    // 时间格式化
                    dateFormat: function (row, column) {
                        var date = row[column.property];
                        if (date === undefined || date === 0) {
                            return '';
                        }
                        return moment(date * 1000).format("YYYY-MM-DD HH:mm:ss");
                    }
                },
                created() {
                    this.loadData(this.search.keyword, this.currentPage, this.pageSize, this.search.sort);
                },
                methods: {
                    // 刷新页面
                    refreshPage() {
                        this.search = {
                            startTime: '',
                            endTime: '',
                            keyword: '',
                            type: '',
                            sort: 'id',
                        };
                        this.loadData(this.search.keyword, this.currentPage, this.pageSize, this.search.sort);
                    },
                    // 确认选择
                    handleSelect(row) {
                        Fast.api.close({
                            id: row.id,
                            nickname: row.nickname,
                            avatar: row.avatar,
                            mobile: row.mobile
                        });
                    },
                    handleClearSelect(field) {
                        // 重新加载
                        this.$set(this.search, field, '');
                        this.loadData(this.search.keyword, this.currentPage, this.pageSize, this.search.sort);
                    },
                    handleClearSearchInp() {
                        if (this.search.type) {
                            // 重新加载
                            this.loadData(this.search.keyword, this.currentPage, this.pageSize, this.search.sort);
                        }
                    },
                    // 从服务器读取数据
                    loadData(key, currentPage, pageSize, sort) {
                        this.listLoading = true;
                        $.getJSON('user/user/select', {
                            keyword: key,
                            type: this.search.type,
                            start_time: this.sendStartTime,
                            end_time: this.sendEndTime,
                            page: currentPage,
                            rows: pageSize,
                            sort: sort,
                        }, data => {
                            this.totalCount = data.total;
                            this.tableData = data.list;
                            this.listLoading = false;
                        });
                    },
                    // 排序
                    sortChange(e) {

                    },
                    // 搜索
                    searchData() {
                        this.sendStartTime = this.search.startTime !== '' ? +this.search.startTime / 1000 : '';
                        this.sendEndTime = this.search.endTime !== '' ? +this.search.endTime / 1000 : '';
                        this.loadData(this.search.keyword, this.currentPage, this.pageSize, this.search.sort);
                    },
                    // 每页显示数据量变更
                    handleSizeChange(val) {
                        this.pageSize = val;
                        this.loadData(this.search.keyword, this.currentPage, this.pageSize, this.search.sort);
                    },
                    // 页码变更
                    handleCurrentChange(val) {
                        this.currentPage = val;
                        this.loadData(this.search.keyword, this.currentPage, this.pageSize, this.search.sort);
                    },
                    // 多选响应
                    handleSelectionChange(val) {
                        this.multipleSelection = val;
                    },
                }
            });
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});